/**
*  表单提交验证JS
*  仲伟涛 2011-03-20
*
*/
var Zvalidator = {
	defaultOption : {
		'imgpath' : "images/"

	},
	regObj : {
		require : /\S+/,
		email   : /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/,
		url     : /^http(s)?:\/\/([A-Za-z0-9\-]+\.)?[A-Za-z0-9\-]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,
		number  : /^\d+$/,
		integer : /^[-\+]?\d+$/,
		doubleN : /^[-\+]?\d+(\.\d+)?$/,
		english : /^[A-Za-z]+$/,
		Benglish: /^[A-Z]+$/,
		chinese :  /^[\u0391-\uFFE5]+$/,
		date    : /^(\d{4})([-./])(\d{1,2})\2(\d{1,2})$/
	},

	//初始化表单元素
	initForm : function() {
		var inputs = document.getElementsByTagName('input');
		this.iniInput(inputs);
		inputs = document.getElementsByTagName('select');
		this.iniInput(inputs);
		inputs = document.getElementsByTagName("textarea");
		this.iniInput(inputs);
	},
	//初始化所有的输入控件
	iniInput : function (inputs){
		for(var i=0; i< inputs.length; i++)	{
			var obj = inputs[i];
			var attr = obj.getAttribute("validator");
			if(typeof attr == 'undefined' || attr == null || attr == '' )return ;

			var errorMsg=document.getElementById(obj.name + '_info');
			if(!errorMsg){
				errorMsg=document.createElement('span');
				errorMsg.id=obj.name + '_info';
				this.insertAfter(errorMsg,obj);
			}
			errorMsg.className = 'validator_blue';
			var inputMsgInfo = obj.getAttribute("info");
			var msgStr  = inputMsgInfo
						? '<img src="'+this.defaultOption.imgpath+'info.gif" align="absmiddle">' +  inputMsgInfo
						: '';
			errorMsg.innerHTML = msgStr ;
			errorMsg.style.display = '';


			//如果初始化的时候input有值，马上验证
			if(obj.type.toUpperCase() == 'TEXT'
			  || obj.type.toUpperCase() == 'PASSWORD'
			  || obj.tagName.toUpperCase() == 'TEXTAREA'
			  || obj.tagName.toUpperCase() == 'SELECT'

			) {
				obj.onblur = this.CheckChange;
				obj.onFocus = this.onFocus;
				if(obj.tagName.toUpperCase() == 'SELECT'){
					obj.onChange = this.CheckChange;
				}
				if(obj.value != '') {
					Zvalidator.CheckObj(obj);
				}
			}
		}

	},

	//验证一个form，如果不传入参数，就验证所有的form
	checkForm : function(formObj){
		var rtn_flag = true;
		var context  = (typeof(context) != 'object') ? document : formObj;

		var inputs   = context.getElementsByTagName('input');
		for(var i=0; i< inputs.length; i++){
			if(!Zvalidator.CheckObj(inputs[i])) rtn_flag=false;
		}
		inputs = context.getElementsByTagName('select');
		for(var i=0; i< inputs.length; i++){
			if(!Zvalidator.CheckObj(inputs[i])) rtn_flag=false;
		}
		inputs = context.getElementsByTagName('textarea');
		for(var i=0; i< inputs.length; i++){
			if(!Zvalidator.CheckObj(inputs[i])) rtn_flag=false;
		}
		return rtn_flag;
	},
	CheckObj : function(obj){
	   var attr = obj.getAttribute("validator");

		if(typeof attr == "undefined" || attr == null || attr == "")return true;
		var rtn_flag=true;
		switch(attr){
			case "require":
			case  "number":
			case  "url":
			case  "url2":
			case  "english":
			case  "date":
			case  "chinese":
			case  "email":
				if(!this.doValidate(this.regObj[attr],obj)){
					rtn_flag=false;
				}
				break;
			case "ajax"://AJAX
				if(obj.value){
					var ajaxUrl = obj.getAttribute("urn");
					ajaxUrl = ajaxUrl.replace("@@",encodeURIComponent(obj.value));
					var d=new Date();
					var tm = d.getTime();
					if(ajaxUrl.indexOf("?") >= 0){
						ajaxUrl += "&ordtm=" + tm
					}else{
						ajaxUrl += "?ordtm=" + tm
					}
					$.get(ajaxUrl,function(data){
						var dataJs = eval("("+data+")");
						if(dataJs.flag == "1"){
							Zvalidator.showMsg(obj,1);
						}else{
							Zvalidator.showMsg(obj,0,dataJs.msg);
						}
					});
				}else{
				   Zvalidator.showMsg(obj,0,'');
				}
				break;
			case  "tel":
				if(!this.doValidate(this.regObj._phone,obj)&&!this.doValidate(this.regObj._mobile,obj)){
					rtn_flag=false;
				}
				break;
			case "password2": //密码重复
				var cmpObjId = obj.getAttribute("cmp");
				var cmpObjVal = cmpObjId ? document.getElementById(cmpObjId).value : '';
				if(cmpObjVal != obj.value){
					this.showMsg(obj,0);
				}else{
					this.showMsg(obj,1);
				}

				break;
			default:
				if(!this.regObj.require.exec(obj.value)){
					this.showMsg(obj,0);
					rtn_flag=false;
				}

		}
		return rtn_flag;
	 },
	//进行正则的验证，并返回正确的信息
	doValidate : function (reg,obj){
		if(reg.exec(obj.value)){//验证OK
			this.showMsg(obj,1);
			return true;
		}else{//验证错误
			this.showMsg(obj,0);
			return false;
		}
	},
	//---------------------
	// 一些共用的函数
	//---------------------
	insertAfter : function(newElement,targetElement){
		var parent = targetElement.parentNode;
		if(parent.lastChild == targetElement){
			parent.appendChild(newElement);
		}else{
			parent.insertBefore(newElement,targetElement.nextSibling);
		}
	},

	CheckChange : function(evnt) {
		if(document.all) {
			obj = event.srcElement;
		} else {
			obj = evnt.target;
		}
		var temptips=document.getElementById(obj.id+'tips');
		if (temptips) {
			temptips.style.display='none';
		}
		Zvalidator.CheckObj(obj);//进行验证对象
	},
	onFocus : function(evnt) {
		if(document.all) {
			obj = event.srcElement;
		} else {
			obj = evnt.target;
		}
		var errorMsg=document.getElementById(obj.name + '_info');
		if(typeof errorMsg != 'undefined') {
			errorMsg.className = 'validator_blue';

			var inputMsgInfo = obj.getAttribute("info");
			var msgStr  = inputMsgInfo
						? '<img src="'+this.defaultOption.imgpath+'info.gif" align="absmiddle">' +  inputMsgInfo
						: '';
			errorMsg.innerHTML = msgStr;
			errorMsg.style.display = '';
		}
	},
	//显示提示信息
	showMsg : function(obj, code,paramMsg) {
		msg = typeof msg == 'undefined' ? '&nbsp;' : msg;
		var errorMsg=document.getElementById(obj.name + '_info');
		if(!errorMsg){
			errorMsg=document.createElement('span');
			errorMsg.id = obj.name + '_info';
			errorMsg.className = 'validator_green';
			this.insertAfter(errorMsg,obj);
		}
		errorMsg.style.display = '';
		if(code == 1) //验证ok的
		{
			var inputMsgInfo = obj.getAttribute("ok");
			var msg = inputMsgInfo ? inputMsgInfo : '&nbsp;';
			msg = paramMsg ? paramMsg : msg;
			errorMsg.className = 'validator_green';
			errorMsg.innerHTML = '<img src="'+this.defaultOption.imgpath+'check_right.gif" align="absmiddle">' + msg;
			return;
		}else if(code==0)//验证error
		 {
			var inputMsgInfo = obj.getAttribute("err");
			var msg = inputMsgInfo ? inputMsgInfo : '&nbsp;';
			msg = paramMsg ? paramMsg : msg;
			errorMsg.className = 'validator_red';
			errorMsg.innerHTML = '<img src="'+this.defaultOption.imgpath+'check_error.gif" align="absmiddle">' + msg;
			return;
		}
	},

	init : function(option){
		this.initForm();
	}



}



